<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>  
    <style>
        .container{
            display: flex;
            justify-content: center;
            position: relative;
        }
        .item{
            width: 100px;
            height: 50px;
            border: 1px solid skyblue;
            margin: 60px 10px 0;
        }
        .page-header{
            position: absolute;
            top: 0;
            border-bottom: 1px solid #999;
        }
    </style>
    <script>
        window.onload=function(){
            //获取item元素
            let item=document.getElementsByClassName("item");
            for(let i=0;i<item.length;i++){
                item[i].onclick=function(){
                    //循环为 Var时不能用item[i].style  i已经为3了
                    item[i].style.backgroundColor="pink";
                }
            }
        }
    </script>
</head>
<body>
    <div class="container">
        <h2 class="page-header">点击切换颜色</h2>
        <div class="item"></div>
        <div class="item"></div>
        <div class="item"></div>
    </div>
</body>
</html>